/**
 * XML请求请求支持中间件，依赖于xml2js来处理 xml 格式内容并且将其转换为 json
 */
import { Logger } from '@nestjs/common';
import { Injectable, NestMiddleware } from '@nestjs/common';

@Injectable()
export class LoggerMiddleware implements NestMiddleware {
  constructor(private readonly logger: Logger) {}

  use(req: any, res: any, next: () => void) {
    next();
    const code = res.statusCode;
    const logData = {
      originalUrl: req.originalUrl,
      Method: req.method,
      Ip: req.ip,
      statusCode: code,
      Parmas: JSON.stringify(req.params),
      Query: JSON.stringify(req.query),
      Body: JSON.stringify(req.body),
    };
    if (code >= 500) {
      this.logger.error(logData);
    } else if (code >= 400) {
      this.logger.warn(logData);
    } else {
      this.logger.log(logData);
    }
  }
}
